#include "algorithm.h"

// sequential search from array; return index of target in array if existed else return -1 for error
int sequential_search(int *array, int target, int len)
{
    for (int index = 0; index < len; index++)
    {
        if (array[index] == target)
            return index;
    }

    return -1;
}

int binary_search(int *array, int target, int len)
{
    int low = 0,
        high = len - 1,
        mid = len / 2;

    while (low <= high)
    {
        mid = (low + high) / 2;
        if (array[mid] == target)
            return mid;
        else if (array[mid] > target)
            high = mid - 1;
        else
            low = mid + 1;
    }

    return -1;
}